package com.cec.controller.admin;


import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.cec.service.admin.UserService;
import com.cec.dto.UserDTO;
import com.cec.dto.UserLoginDTO;
import com.cec.entity.User;
import com.cec.properties.JwtProperties;
import com.cec.result.Result;
import com.cec.utils.JwtUtil;
import com.cec.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lt
 * @since 2024-10-25
 */
@Slf4j
@RestController("adminController")
@RequestMapping("/admin")
@Api(tags = "管理员管理")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    @ApiOperation("管理员登陆")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        User user = userService.login(userLoginDTO);
        Map<String,Object> claim=new HashMap<>();
        claim.put("adminId",user.getId());
        String token = JwtUtil.createToken(jwtProperties.getAdminOutTime(),jwtProperties.getAdminSecretKey(),claim);
        UserLoginVO userLoginVO = new UserLoginVO(userLoginDTO.getUserName(),user.getImg(),token);
        return Result.success(userLoginVO);
    }

    @ApiOperation("用户退出")
    @PostMapping("/logout")
    public Result logout() {
        return Result.success();
    }


    @GetMapping("/pageQuery")
    @ApiOperation("分页查询")
    public Result pageQueryUserList(UserDTO userDTO){
        log.info("pageQueryDTO:{}", userDTO);
        return userService.pageQueryUserList(userDTO);
    }

    @PostMapping
    @ApiOperation("添加用户")
    public Result save(@RequestBody UserDTO userDTO){
        return userService.saveUser(userDTO);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户信息")
    public Result queryUserById(@PathVariable Long id){
        User user = userService.queryUserById(id);
        return Result.success(user);
    }

    @PutMapping
    @ApiOperation("修改用户信息")
    public Result updateUser(@RequestBody UserDTO userDTO){
        log.info("userDTO:{}",userDTO);
        userService.updateUser(userDTO);
        return Result.success();
    }

    @DeleteMapping("/logicDel/{id}")
    @ApiOperation("逻辑删除用户信息")
    public Result logicDeleteUserById(@PathVariable Integer id){
        return userService.logicDeleteUserById(id);
    }

    @DeleteMapping("/logicBatchDel")
    @ApiOperation("逻辑批量删除用户信息")
    public Result logicBatchDeleteUserByIds(@RequestBody List<Integer> ids){
        return userService.logicBatchDeleteUserByIds(ids);
    }

    @GetMapping("/getRole")
    @ApiOperation("获取角色")
    public Result getRole(){
        return userService.getRole();
    }
}

